package com.hulushuju.core.net;

import com.hulushuju.core.net.proxy.Proxy;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import okhttp3.Headers;

/**
 * @Author: Hanjiafeng
 * @Date: 2019-05-27
 * @Desc: 请求参数
 */
public class HttpParameter {

    /**
     * Cookie管理器,用于Cookie对象的存储与应用
     */
//    @Getter
//    private  CookieManager cookieManager = CookieManager.getInstance(OkCookieManagerImpl.class);

    /**
     * 请求头
     */
    private final Headers.Builder headersBuilder = new Headers.Builder();

    /**
     * 请求持续时间,单位秒
     */
    @Getter
    @Setter
    private long requestTimeout;

    /**
     * 是否支持重定向
     */
    @Getter
    @Setter
    private boolean followRedirect;

    /**
     * 是否支持从Https->Http或者Http-Https的重定向
     */
    @Getter
    @Setter
    private boolean followSslRedirect;

    /**
     * 代理
     */
    @Getter
    @Setter
    private Proxy proxy;

    /**
     * 请求方法
     */
    @Getter
    @Setter
    private Method method;

    private HttpParameter(long requestTimeout, boolean followRedirect, boolean followSslRedirect, Proxy proxy, Method method) {
        //预置请求头
//        headersBuilder
//                .add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
//                .add("Accept-Encoding", "gzip, deflate, br")
//                .add("Content-Type", "application/x-www-form-urlencoded")
//                .add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3724.8 Safari/537.36");

        this.requestTimeout = requestTimeout;
        this.followRedirect = followRedirect;
        this.followSslRedirect = followSslRedirect;
        this.proxy = proxy;
        this.method = method;
    }

    /**
     * 获取请求头
     *
     * @return 请求头
     */
    Headers getHeaders() {
        return headersBuilder.build();
    }

    /**
     * 往key对应的value中添加一个value
     *
     * @param key   key
     * @param value value
     */
    void addHeader(String key, String value) {
        headersBuilder.add(key, value);
    }

    /**
     * 使用指定value替换key对应的value
     *
     * @param key   可以
     * @param value value
     */
    void setHeader(String key, String value) {
        headersBuilder.set(key, value);
    }

    /**
     * 获取key对应的value
     *
     * @param key key
     * @return value
     */
    String getHeader(String key) {
        return headersBuilder.get(key);
    }

    /**
     * 移除key及其value
     *
     * @param key key
     */
    void removeHeader(String key) {
        headersBuilder.removeAll(key);
    }

    /**
     * 请求方法枚举
     */
    @AllArgsConstructor
    public enum Method {
        /**
         * Get
         */
        GET("Get"),
        /**
         * Post
         */
        POST("Post");
        @Getter
        private String method;
    }

    static class Builder {
        private long requestTimeout = 30;
        private boolean followRedirect = true;
        private boolean followSslRedirect = true;
        private Method method = Method.GET;
        private Proxy proxy;

        public Builder requestTimeout(long requestTimeout) {
            this.requestTimeout = requestTimeout;
            return this;
        }

        public Builder followRedirect(boolean followRedirect) {
            this.followRedirect = followRedirect;
            return this;
        }

        public Builder followSslRedirect(boolean followSslRedirect) {
            this.followSslRedirect = followSslRedirect;
            return this;
        }

        public Builder proxy(Proxy proxy) {
            this.proxy = proxy;
            return this;
        }

        public Builder method(Method method) {
            this.method = method;
            return this;
        }

        public HttpParameter build() {
            HttpParameter httpParameter = new HttpParameter(requestTimeout, followRedirect, followSslRedirect, proxy, method);
            return httpParameter;
        }
    }
}
